﻿using UnityEngine;
using UnityEditor;
using CommonClass;
using System.Collections.Generic;
using System;

public class TransformComparer : IComparer<Transform>
{
    public int Compare(Transform x, Transform y)
    {
        return string.Compare(x.name, y.name);
    }
}

public class GameObjectRename : EditorWindow
{
    private GameObject curSelect;
    private string objectName;
    private Tuple<string, Transform> tupleResult = new Tuple<string, Transform>("", null);

    #region 重命名属性
    private string tempName;    // 临时的名字 
    private int startIndex;     // 开始的编号 
    private int increment;      // 增量 
    private int digit;          // 位数 
    private bool completion;    // 不足位补齐 
    private bool isSort;        // 是否需要排序 
    #endregion


    public static void ShowWindow()
    {
        EditorWindow window = GetWindow<GameObjectRename>();
        window.position = new Rect(100, 100, 800, 300);
        window.titleContent = new GUIContent("批量重命名工具");
        window.Show();
    }

    void Awake()
    {
        tempName = "";
        startIndex = 1;
        increment = 1;
        digit = 1;
        completion = true;
        curSelect = null;
        isSort = true;
        objectName = "";
    }

    /// <summary>
    /// 使用递归输出GameObject完整路径。
    /// </summary>
    /// <param name="something"></param>
    /// <returns></returns>
    private Tuple<string, Transform> GetGameObjectPath(Tuple<string, Transform> something)
    {
        string path = something.Item1.ToString();
        Transform ob = something.Item2 as Transform;
        if (ob == null)
        {
            return new Tuple<string, Transform>("/" + path, ob);
        }
        else
        {
            return GetGameObjectPath(new Tuple<string, Transform>(path.Equals("") ? ob.name : ob.name + "/" + path, ob.transform.parent));
        }
    }

    void Rename()
    {
        if (tempName.IndexOf('*') == -1)
        {
            ShowNotification(new GUIContent("没有找到可替换字符 * ，请重新输入"));
            return;
        }

        Transform[] childList = curSelect.transform.GetChildArray();
        if (isSort)
        {
            System.Array.Sort(childList, new TransformComparer());
        }

        string tempIndex = "";
        for (int i = 0, count = childList.Length; i < count; i++)
        {
            if (completion)
            {
                tempIndex = (startIndex + (i * increment)).ToString().PadLeft(digit, '0');
            }
            else
            {
                tempIndex = (startIndex + (i * increment)).ToString();
            }
            childList[i].name = tempName.Replace("*", tempIndex);
        }
    }

    void OnGUI()
    {
        EditorGUILayout.HelpBox("GameObject批量重命名工具\n目前只支持二级子物体的重命名", MessageType.Warning);
        GUILayout.Space(3);

        EditorTools.Label("开始重命名", Color.yellow, 20, TextAnchor.MiddleCenter);
        GUILayout.Space(3);
        GUILayout.BeginVertical();

        GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20));
        {
            EditorGUILayout.TextField(new GUIContent("GameObject路径", "在Hierarchy中选取GameObject，然后点击获取路径即可。"), objectName);
            if (GUILayout.Button("获取路径", GUILayout.Width(60)))
            {
                if (Selection.activeGameObject)
                {
                    curSelect = Selection.activeGameObject;
                    tupleResult = GetGameObjectPath(new Tuple<string, Transform>("", curSelect.transform));
                    objectName = tupleResult.Item1;
                }
                else
                {
                    curSelect = null;
                    objectName = "";
                }
            }
        }
        GUILayout.EndHorizontal();

        if (curSelect != null)
        {
            if (curSelect.transform.childCount > 0)
            {
                GUILayout.Space(10);
                EditorTools.Label("详细设置", Color.green, 16, TextAnchor.MiddleCenter);
                GUILayout.Space(3);

                GUILayout.BeginHorizontal(GUILayout.Width(600));
                {
                    GUILayout.BeginVertical("AS TextArea", GUILayout.Height(120));
                    {
                        GUILayout.Space(10);
                        EditorGUILayout.LabelField("设置说明：使用 * 号代替编号。例如 Image*，那么，将得到 Image 1");
                        tempName = EditorGUILayout.TextField(new GUIContent("名称设置"), tempName);

                        GUILayout.Space(3);
                        GUILayout.BeginHorizontal();
                        startIndex = EditorGUILayout.IntField("开始编号", startIndex, GUILayout.Width(200));
                        GUILayout.Space(100);
                        increment = EditorGUILayout.IntField(new GUIContent("增量", "增量从1开始，不支持负数。请使用 1-无穷大"), increment, GUILayout.Width(200));
                        GUILayout.EndHorizontal();

                        GUILayout.Space(3);
                        GUILayout.BeginHorizontal();
                        digit = EditorGUILayout.IntField(new GUIContent("位数", "只支持一位以上的位数"), digit, GUILayout.Width(200));
                        GUILayout.Space(100);
                        completion = EditorGUILayout.Toggle("不足位补齐", completion, GUILayout.Width(200));
                        GUILayout.EndHorizontal();

                        GUILayout.Space(3);
                        GUILayout.BeginHorizontal();
                        isSort = EditorGUILayout.Toggle(new GUIContent("先排序，再重命名", "排序只能从小到大排"), isSort, GUILayout.Width(200));
                        GUILayout.EndHorizontal();
                    }
                    GUILayout.EndVertical();

                    GUILayout.Space(70);
                    if (GUILayout.Button("执行", GUILayout.Width(150), GUILayout.Height(114)))
                    {
                        Rename();
                    }
                }
                GUILayout.EndHorizontal();
            }
            else
            {
                GUILayout.Space(100);
                EditorTools.Label(curSelect.name + " 节点下没有子物体，请重新选择。", Color.red, 20, TextAnchor.MiddleCenter);
            }
        }
        else
        {
            GUILayout.Space(100);
            EditorTools.Label("请先选择GameObject。", Color.red, 20, TextAnchor.MiddleCenter);
        }

        GUILayout.EndVertical();
    }
}
